package com.ctshk.rpc.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.train.dto.TrainTicketsInitializationSettingsDTO;
import com.ctshk.rpc.train.entity.TrainTicketsInitializationSettings;
import com.ctshk.rpc.train.mapper.TrainTicketsInitializationSettingsMapper;
import com.ctshk.rpc.train.req.TrainTicketsInitializationSettingsReq;
import com.ctshk.rpc.train.service.ITrainTicketsInitializationSettingsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 火车票api初始化设置表 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-10
 */
@DubboService
public class TrainTicketsInitializationSettingsServiceImpl extends ServiceImpl<TrainTicketsInitializationSettingsMapper, TrainTicketsInitializationSettings> implements ITrainTicketsInitializationSettingsService {

    /**
     * 火车票api初始化设置
     * @param initializationSettingsReq
     * @param user
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public Result saveSettingInfo(TrainTicketsInitializationSettingsReq initializationSettingsReq, TokenUser user) {

        if(user == null){
            return Result.failed(SystemError.USER_1000);
        }

        TrainTicketsInitializationSettings initializationSettings = new TrainTicketsInitializationSettings();

        //根据供应商id查询火车票api初始化设置是否存在
        List<TrainTicketsInitializationSettings> initializationSettingsList = this.baseMapper.selectList(new QueryWrapper<TrainTicketsInitializationSettings>().lambda()
                .eq(TrainTicketsInitializationSettings::getSupplierId, initializationSettingsReq.getSupplierId())
                .eq(TrainTicketsInitializationSettings::getIsDeleted, IsDeletedCode.NO.getCode()));

        if (CollectionUtils.isNotEmpty(initializationSettingsList)) {
            //存在修改
            initializationSettings = initializationSettingsList.get(0);
            BeanUtils.copyProperties(initializationSettingsReq, initializationSettings);
            initializationSettings.setModifiedId(user.getId());
            initializationSettings.setGmtModified(LocalDateTime.now());
            this.baseMapper.updateById(initializationSettings);
        } else {
            //不存在则新增
            BeanUtils.copyProperties(initializationSettingsReq, initializationSettings);
            initializationSettings.setId(SnowflakeIdWorker.nextId());
            initializationSettings.setCreateId(user.getId());
            initializationSettings.setGmtCreate(LocalDateTime.now());
            initializationSettings.setIsDeleted(IsDeletedCode.NO.getCode());
            this.baseMapper.insert(initializationSettings);
        }
        return Result.success();
    }


    /**
     * 根据供应商id查询火车票api设置
     * @param supplierId
     * @return
     */
    @Override
    public TrainTicketsInitializationSettingsDTO queryDetail(Long supplierId) {

        //通过供应商id查询api设置
        List<TrainTicketsInitializationSettings> initializationSettingsList = this.baseMapper.selectList(new QueryWrapper<TrainTicketsInitializationSettings>().lambda()
                .eq(TrainTicketsInitializationSettings::getSupplierId, supplierId)
                .eq(TrainTicketsInitializationSettings::getIsDeleted, IsDeletedCode.NO.getCode()));

        if (CollectionUtils.isNotEmpty(initializationSettingsList)) {
            TrainTicketsInitializationSettings initializationSettings = initializationSettingsList.get(0);
            TrainTicketsInitializationSettingsDTO initializationSettingsDTO = new TrainTicketsInitializationSettingsDTO();
            BeanUtils.copyProperties(initializationSettings, initializationSettingsDTO);
            return initializationSettingsDTO;
        }
        return null;
    }
}
